<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('文档列表')" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>文档分类：</label>
                                <select name="categoryId" class="form-control">
                                    <option value="">所有分类</option>
                                </select>
                            </li>
                            <li>
                                <label>文档标题：</label>
                                <input type="text" name="title"/>
                            </li>
                            <li>
                                <label>文件名称：</label>
                                <input type="text" name="fileName"/>
                            </li>
                            <li>
                                <label>文件大小(字节)：</label>
                                <input type="text" name="fileSize"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:document:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:document:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:document:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:document:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('system:document:edit')}]];
        var removeFlag = [[${@permission.hasPermi('system:document:remove')}]];
        var prefix = ctx + "system/document";

        $(function() {
            // 加载文档分类下拉框
            $.ajax({
                url: prefix + "/categories",
                type: "get",
                dataType: "json",
                success: function(data) {
                    if (data.code == 0 && data.data) {
                        $.each(data.data, function(index, category) {
                            $("select[name='categoryId']").append(
                                $("<option></option>").val(category.categoryId).text(category.categoryName)
                            );
                        });
                        $("select[name='categoryId']").select2({
                            width: "200px"
                        });
                    }
                }
            });
            
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "文档",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'documentId',
                    title: '文档ID',
                    visible: false
                },
                {
                    field: 'categoryId',
                    title: '分类ID',
                    visible: false
                },
                {
                    field: 'categoryName',
                    title: '所属分类'
                },
                {
                    field: 'title',
                    title: '文档标题'
                },
                {
                    field: 'fileName',
                    title: '文件名称'
                },
                {
                    field: 'filePath',
                    title: '文件存储路径'
                },
                {
                    field: 'fileSize',
                    title: '文件大小',
                    formatter: function(value, row, index) {
                        return formatFileSize(value);
                    }
                },
                {
                    field: 'fileType',
                    title: '文件类型'
                },
                {
                    field: 'createTime',
                    title: '创建时间'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.documentId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.documentId + '\')"><i class="fa fa-remove"></i>删除</a> ');
                        // 如果有文件路径，提供下载按钮
                        if (row.filePath) {
                            actions.push('<a class="btn btn-info btn-xs" href="' + ctx + 'common/download?fileName=' + encodeURI(row.filePath) + '&delete=false"><i class="fa fa-download"></i>下载</a>');
                        }
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
        
        // 格式化文件大小
        function formatFileSize(bytes) {
            if (bytes === 0) return '0 B';
            var k = 1024;
            var sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
            var i = Math.floor(Math.log(bytes) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }
    </script>
</body>
</html>